package com.atguigu.gmall.payment.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePagePayResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.atguigu.gmall.payment.service.ZfbPayService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/10/15 15:50 星期六
 * description:
 */
@Service
public class ZfbPayServiceImpl implements ZfbPayService {

    /**
     * 支付宝url
     */
    @Value("${alipay_url}")
    private String alipayUrl;

    /**
     * 支付宝appId
     */
    @Value("${app_id}")
    private String appId;
    /**
     * 支付宝提供的私钥
     */
    @Value("${app_private_key}")
    private String appPrivateKey;
    /**
     * 支付宝提供的公钥
     */
    @Value("${alipay_public_key}")
    private String alipayPublicKey;
    /**
     * 同步回调地址: 用户支付完成以后支付宝引导用户跳转的地址
     */
    @Value("${return_payment_url}")
    private String returnPaymentUrl;

    /**
     * 支付宝异步通知地址
     */
    @Value("${notify_payment_url}")
    private String notifyPaymentUrl;

    /**
     * 获取支付宝支付的二维码地址
     *
     * @param body    订单描述
     * @param orderId
     * @param money
     * @return
     */
    @Override
    public String getPayPage(String body, String orderId, String money) {
        //客户端对象初始化
        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayUrl,
                appId,
                appPrivateKey,
                "json",
                "utf-8",
                alipayPublicKey,
                "RSA2");
        //请求对象初始化
        AlipayTradePagePayRequest request =
                new AlipayTradePagePayRequest();
        //异步通知地址
        request.setNotifyUrl(notifyPaymentUrl);
        //同步回调地址
        request.setReturnUrl(returnPaymentUrl);
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        bizContent.put("total_amount", money);
        bizContent.put("subject", body);
        bizContent.put("product_code", "FAST_INSTANT_TRADE_PAY");
        request.setBizContent(bizContent.toString());
        try {
            //发起请求
            AlipayTradePagePayResponse response = alipayClient.pageExecute(request);
            if(response.isSuccess()){
                return response.getBody();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 根据订单号查询订单的支付结果
     *
     * @param orderId
     * @return
     */
    @Override
    public String getPayResult(String orderId) {

        AlipayClient alipayClient = new DefaultAlipayClient(
                alipayUrl,
                appId,
                appPrivateKey,
                "json",
                "utf-8",
                alipayPublicKey,
                "RSA2");
        AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
        JSONObject bizContent = new JSONObject();
        bizContent.put("out_trade_no", orderId);
        request.setBizContent(bizContent.toString());
        try {
            AlipayTradeQueryResponse response = alipayClient.execute(request);
            if(response.isSuccess()){
                return response.getBody();
            }
        }catch (Exception e){
            e.printStackTrace();
        }

        return null;
    }
}
